/**
 * Copyright &copy; 2012-2013 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.thinkgem.jeesite.common.utils;

import java.text.DateFormat;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.xml.crypto.Data;

import org.apache.commons.lang.time.DateFormatUtils;

import com.alibaba.druid.sql.visitor.functions.Now;

/**
 * 日期工具类, 继承org.apache.commons.lang.time.DateUtils类
 * @author ThinkGem
 * @version 2013-3-15
 */
public class DateUtils extends org.apache.commons.lang.time.DateUtils {
	
	private static String[] parsePatterns = { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss","yyyy-MM-dd hh:mm:ss","yyyyMMdd" };

	/**
	 * 得到当前日期字符串 格式（yyyy-MM-dd）
	 */
	public static String getDate() {
		return getDate("yyyy-MM-dd");
	}
	
	/**
	 * 得到指定日期字符串 格式（yyyy-MM-dd）
	 */
	public static String getDate(Date date,String pattern) {
		return getDate(pattern);
	}
	
	public static String getStrDate(Date date,String pattern) {
		return DateFormatUtils.format(date, pattern);
	}
	
	public static String getNumberByDate1(Date time) {
		return formatDate(time,"MM月dd日 HH:mm:ss");
	}
	
	/**
	 * @author Jakemanse
	 * @time 2013-9-28  下午1:55:30
	 * @function <p> 获取延迟N天后的 日期时间 </p>
	 * @param days
	 * @return
	 */
	public static Date getDelayDate(int days){
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DAY_OF_MONTH, days);
		Date date = calendar.getTime();
		date.setHours(23);
		date.setMinutes(59);
		date.setSeconds(59);
		
		return date;
	}
	
	/**
	 * @author Jakemanse
	 * @time 2013-9-28  下午1:55:30
	 * @function <p> 获取延迟N月后的 日期时间 </p>
	 * @param days
	 * @return
	 */
	public static Date getDelayMonth(int month){
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MONTH, month);
		Date date = calendar.getTime();
		date.setHours(23);
		date.setMinutes(59);
		date.setSeconds(59);
		
		return date;
	}
	
	/**
	 * @author Jakemanse
	 * @time 2013-9-28  下午1:55:30
	 * @function <p> 获取延迟N天后的 日期时间 </p>
	 * @param days
	 * @return
	 */
	public static Date getDelayDate0(int days){
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DAY_OF_MONTH, days);
		Date date = calendar.getTime();
		date.setHours(0);
		date.setMinutes(0);
		date.setSeconds(0);
		return date;
	}
	
	/**
	 * 得到当前日期字符串 格式（yyyy-MM-dd） pattern可以为："yyyy-MM-dd" "HH:mm:ss" "E"
	 */
	public static String getDate(String pattern) {
		return DateFormatUtils.format(new Date(), pattern);
	}
	
	/**
	 * 得到日期字符串 默认格式（yyyy-MM-dd） pattern可以为："yyyy-MM-dd" "HH:mm:ss" "E"
	 */
	public static String formatDate(Date date, Object... pattern) {
		String formatDate = null;
		if (pattern != null && pattern.length > 0) {
			formatDate = DateFormatUtils.format(date, pattern[0].toString());
		} else {
			formatDate = DateFormatUtils.format(date, "yyyy-MM-dd");
		}
		return formatDate;
	}

	/**
	 * 得到当前时间字符串 格式（HH:mm:ss）
	 */
	public static String getTime() {
		return formatDate(new Date(), "HH:mm:ss");
	}


	/**
	 * 得到当前日期和时间字符串 格式（yyyy-MM-dd HH:mm:ss）
	 */
	public static String getDateTime() {
		return formatDate(new Date(), "yyyy-MM-dd HH:mm:ss");
	}
	
	/**
	 * 得到指定日期和时间字符串 格式（yyyy-MM-dd HH:mm:ss）
	 */
	public static String getDateTime(Date date) {
		return formatDate(date, "yyyy-MM-dd HH:mm:ss");
	}
	
	/**
	 * 得到当前日期和时间字符串 格式（yyyyMMddHHmmss）
	 */
	public static String getDateTimeString() {
		return formatDate(new Date(), "yyyyMMddHHmmss");
	}

	/**
	 * 得到当前年份字符串 格式（yyyy）
	 */
	public static String getYear() {
		return formatDate(new Date(), "yyyy");
	}

	/**
	 * 得到当前月份字符串 格式（MM）
	 */
	public static String getMonth() {
		return formatDate(new Date(), "MM");
	}

	/**
	 * 得到当天字符串 格式（dd）
	 */
	public static String getDay() {
		return formatDate(new Date(), "dd");
	}
	
	/**
	 * @author Jakemanse
	 * @time 2013-9-9  上午9:49:12
	 * @function <p> 得到当天字符串格式如  20130831 </p>
	 * @return
	 */
	public static String getTodayString(){
		return formatDate(new Date(), "yyyyMMdd");
	}

	/**
	 * 得到当前星期字符串 格式（E）星期几
	 */
	public static String getWeek() {
		return formatDate(new Date(), "E");
	}
	
	/**
	 * 日期型字符串转化为日期 格式（"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss.fff" ）
	 */
	public static Date parseDate(String str) {
		try {
			return parseDate(str, parsePatterns);
		} catch (ParseException e) {
			return null;
		}
	}
	

	/**
	 * 获取过去的天数
	 * @param date
	 * @return
	 */
	public static long pastDays(Date date) {
		long t = new Date().getTime()-date.getTime();
		return t/(24*60*60*1000);
	}
	
	/**
	 * 获取过去的天数
	 * @param date
	 * @return
	 */
	public static long pastHours(Date date) {
		long t = new Date().getTime()-date.getTime();
		return t/(60*60*1000);
	}
	
    /**
	* 取本周7天的第一天（周一的日期）
	*/
	public static Date getNowWeekBegin() {
		Calendar c = new GregorianCalendar();
       c.setFirstDayOfWeek(Calendar.MONDAY);
       c.setTime(new Date());
       c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); // Monday
		Date monday = c.getTime();
		
		
		DateFormat df = DateFormat.getDateInstance();
		String preMonday = df.format(monday);
		
		return parseDate(preMonday + " 00:00:00");
	
	}
	
	/**
	* 取本周7天的最后一天天（周一的日期）
	*/
	public static Date getNowWeekEnd() {
		Calendar c = new GregorianCalendar();
       c.setFirstDayOfWeek(Calendar.MONDAY);
       c.setTime(new Date());
       c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6); // Sunday
		Date monday = c.getTime();
		
		DateFormat df = DateFormat.getDateInstance();
		String preMonday = df.format(monday);
		
		Date date = c.getTime();
	    date.setHours(23);
	    date.setMinutes(59);
	    date.setSeconds(59);
		
	    return date;
		
		
		//return parseDate(preMonday + " 23:59:59");
	
	}
	
	
	/**   
	 * 得到本月的第一天   
	 * @return   
	 */    
	public static Date getMonthFirstDay() {     
	    Calendar calendar = Calendar.getInstance();     
	    calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));     
	    
	    
	    DateFormat df = DateFormat.getDateInstance();
		String preMonday = df.format(calendar.getTime());
		
		
		Date date = calendar.getTime();
	    date.setHours(0);
	    date.setMinutes(0);
	    date.setSeconds(0);
		return date;
		
		//return parseDate(preMonday + " 00:00:00");
	}     
	
	
	/***
	 * 得到本月的最后一天  
	 *@author xiaoqian
	 * @return
	 * 2014-6-16下午3:52:41
	 */
	public static Date getMonthLastDay() {     
	    Calendar calendar = Calendar.getInstance();     
	    calendar.set(Calendar.DAY_OF_MONTH, calendar     
	            .getActualMaximum(Calendar.DAY_OF_MONTH));     
	    
	    Date date = calendar.getTime();
	    date.setHours(23);
	    date.setMinutes(59);
	    date.setSeconds(59);
	    
//	    	DateFormat df = DateFormat.getDateInstance();
//	  		String lastDayTime = df.format(calendar.getTime());
	  	return date;
	}  
	
	
	
	public static String getYesterday(String format){
		Calendar cal=Calendar.getInstance();
        cal.add(Calendar.DATE,-1);
        Date d=cal.getTime();

		 SimpleDateFormat sp=new SimpleDateFormat(format);
		return sp.format(d);
	}
	
	/***
	 * 得到当前时间和本月最后一天的时间差  
	 *@author xiaoqian
	 * @return
	 * 2014-6-16下午3:52:41
	 */
	public static Integer getMonthLastDayCountByNow() {     
		long diff =getMonthLastDay().getTime()-new Date().getTime();
		long days = diff / (1000 * 60 * 60 * 24);
	  		return (int)days;
	}  
	
	/***
	 * 得到当前时间和本周最后一天的时间差  
	 *@author xiaoqian
	 * @return
	 * 2014-6-16下午3:52:41
	 */
	public static Integer getWeeksLastDayCountByNow() {     
		long diff =getMonthLastDay().getTime()-new Date().getTime();
		long days = diff / (1000 * 60 * 60 * 24);
	  		return (int)days;
	}  
	
	 /**  
     * 计算两个日期之间相差的天数  
     * @param smdate 较小的时间 
     * @param bdate  较大的时间 
     * @return 相差天数 
     * @throws ParseException  
     */    
    public static int daysBetween(Date smdate,Date bdate)    
    {    
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");  
        try {
			smdate=sdf.parse(sdf.format(smdate));
			bdate=sdf.parse(sdf.format(bdate));  
		} catch (ParseException e) {
			e.printStackTrace();
		}  
        Calendar cal = Calendar.getInstance();    
        cal.setTime(smdate);    
        long time1 = cal.getTimeInMillis();                 
        cal.setTime(bdate);    
        long time2 = cal.getTimeInMillis();         
        long between_days=(time2-time1)/(1000*3600*24);  
            
       return Integer.parseInt(String.valueOf(between_days));           
    }    
    
    //Java 取得指定日期之后N天的日期 zz
    public static Date afterNDay(Date date,int n){   
    	Calendar c=Calendar.getInstance();   
    	c.setTime(date);   
    	c.add(Calendar.DATE,n);   
    	return c.getTime();   
    	} 
    
    //Java 取得指定时间 几分钟 之后的时间
    public static Date afterMinute(Date date,int n){
    	Calendar c=Calendar.getInstance();   
    	c.setTime(date);
    	c.add(Calendar.MINUTE,n);   
    	return c.getTime();   
    } 
    
    //周几
    public static int getWeek(Date date) {
    	  Calendar cal = Calendar.getInstance();
    	  cal.setTime(date);
    	  return cal.get(Calendar.DAY_OF_WEEK);
    	 }
    
    //周几
    public static String getStrWeek(Date date) {
    	Calendar cal = Calendar.getInstance();
    	cal.setTime(date);
    	int week = cal.get(Calendar.DAY_OF_WEEK);
    	String weeks[]={"","日","一","二","三","四","五","六"};
    	
    	return weeks[week];
    }
    
    public static int getDay(Date date) {
    	Calendar cal = Calendar.getInstance();
    	cal.setTime(date);
    	return cal.get(Calendar.DAY_OF_MONTH);
    }
    
    //当天为第几周
    public static int getWeekCi(Date date) {
    	
    	Calendar cal=Calendar.getInstance(); 
    	cal.setTime(date);
    	int week = cal.get(Calendar.DAY_OF_WEEK);
    	return week==1?cal.get(Calendar.WEEK_OF_YEAR)-1:cal.get(Calendar.WEEK_OF_YEAR);
    }
    
	
    
    public static Date getDateBystr(String datestr,String prm) {
    	 SimpleDateFormat sdf =   new SimpleDateFormat( prm );
    	 Date date = null;
    	 try {
    		 date =sdf.parse(datestr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
    	 return date;
    }
    
    /**
     * 计算两时间相差分钟
     * @param date1
     * @param date2
     * @return
     */
    public static Long  getMinute(Date date1,Date date2) {
		Long l=date1.getTime()-date2.getTime();
		return l/1000/60;
   }
    
	/**
	 * @param args
	 * @throws ParseException
	 */
	public static void main(String[] args) throws ParseException {
//		System.out.println(formatDate(parseDate("2010/3/6")));
//		System.out.println(getDate("yyyy年MM月dd日 E"));
//		long time = new Date().getTime()-parseDate("2012-11-19").getTime();
//		System.out.println(time/(24*60*60*1000));
		
//		System.out.println(pastDays(date));
		
		
		
		System.out.println(formatDate(parseDate("2017/5/9"),"yyyy/MM/dd"));
		
	}
}
